﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using SocialNetwork.Models;
using System.Web.Helpers;

namespace SocialNetwork.Controllers
{
    public class AdministratorController : Controller
    {
        public aMembershipProvider MembershipService;
        public aRoleProvider AuthorizationService;

        protected override void Initialize(RequestContext requestContex)
        {
            if (MembershipService == null)
                MembershipService = new aMembershipProvider();
            if (AuthorizationService == null)
                AuthorizationService = new aRoleProvider();
            
            base.Initialize(requestContex);
        }

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Administrator, koyot")]
        public ActionResult UsersList()
        {
            var users = MembershipService.GetAllUsers();
            return View(users);
        }

        public ActionResult UserDetails(Guid id)
        {
            var model = MembershipService.GetUsersByCondition(u => u.Id == id, x => x.Username).FirstOrDefault();
            return View(model);
        }

        #region CREATE USER

        [Authorize(Roles = "Administrator, koyot")]
        public ActionResult CreateUser()
        {
            return View();
        }
        
        [HttpPost]
        [Authorize(Roles = "Administrator, koyot")]
        public ActionResult CreateUser(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.CreateUser(model, "User"))
                {
                    return RedirectToAction("UsersList");
                }
                else
                {
                    ModelState.AddModelError("", "*");
                }
            }
            return View(model);
        }
        #endregion

        #region EDIT USER
        
        public ActionResult EditUser(Guid id)
        {
            var model = MembershipService.GetUsersByCondition(u => u.Id == id, o => o.Username).FirstOrDefault();
            return View(model);
        }
        
        [HttpPost]
        public ActionResult EditUser(User model)
        {
            if (ModelState.IsValid)
            {
                MembershipService.UpdateUser(model);
                return Redirect("UsersList");
            }
            
            return View(model);
        }

        #endregion
        
        #region DELETE USER
 
        public ActionResult DeleteUser(Guid id)
        {
            var user = MembershipService.GetUser(id);
            return View(user);
        }

        [HttpPost]
        public ActionResult DeleteUser(string username)
        {
            MembershipService.DeleteUser(username);
            return RedirectToAction("UsersList");
        }

        #endregion

        
        [HttpPost]
        public ActionResult DeleteAdmin(Guid id)
        {
            var model = MembershipService.GetUsersByCondition(u => u.Id == id, o => o.Username);
            return View(model);
        }
    }
}
